using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml.XPath;

namespace CriticalSystems.Instrumentation
{
    internal sealed class SubsystemInformation
    {
        private const string DefaultSubsystemInstanceName = "Default";

        public SubsystemInformation(XPathNavigator navigator)
        {
            Subsystem = navigator.GetAttribute("name", string.Empty);
            CategoryId = int.Parse(navigator.GetAttribute("categoryId", string.Empty));
            SubsystemType = (SubsystemType)Enum.Parse(typeof(SubsystemType), navigator.GetAttribute("type", string.Empty));
            Instances = new List<string>();
        }

        public SubsystemType SubsystemType { get; private set; }

        public int CategoryId { get; private set; }

        public string Subsystem { get; private set; }
        
        public IList<string> Instances { get; private set; }

        public void AddInstance(string instance)
        {
            if (string.IsNullOrEmpty(instance))
            {
                instance = DefaultSubsystemInstanceName;
            }
            if (Instances.Contains(instance))
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
                                                          "Instance {0} already exists for subsystem {1}", instance, Subsystem));
            }
            Instances.Add(instance);
        }
    }
}